#!/bin/bash

if [ -f /mnt/geoserver_datadir/global.xml ]
then
    echo 'Datadir already initialized'
else
    echo 'Initializing datadir from georchestra/geoserver_minimal_datadir'

    cp -Rax /var/local/geoserver.orig/* /mnt/geoserver_datadir/
    cp -Rax /var/local/geoserver.orig/.git /mnt/geoserver_datadir/
    cp -Rax /var/local/geoserver.orig/.gitignore /mnt/geoserver_datadir/

    echo 'Change LDAP url according to docker setup'
    sed -i 's:<serverURL>ldap\://localhost\:389</serverURL>:<serverURL>ldap\://ldap\:389</serverURL>:g' /mnt/geoserver_datadir/security/role/my_ldap/config.xml
    sed -i 's:<serverURL>ldap\://localhost\:389</serverURL>:<serverURL>ldap\://ldap\:389</serverURL>:g' /mnt/geoserver_datadir/security/usergroup/my_ldap/config.xml

    echo 'Change rootLogger config according to docker setup'
    sed -i 's:log4j.rootLogger=WARN, geoserverlogfile, stdout:log4j.rootLogger=WARN, stdout:g' /mnt/geoserver_datadir/logs/*
fi
